﻿using d1Common.WebController;
using d1Project.Modules.Business.doXZ.Domain;
using d1Project.Modules.Business.doXZ.Models;
using d1Project.Tools;
using d1Project.Tools.PagedList;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Business.doXZ.Controllers.a1
{
    /// <summary>
    /// 薪酬对比
    /// </summary>
    [Route("doXZ/XZ_a1_Xcdb")]
    public class XZ_a1_XcdbController : ControllerBase
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private doXZDbContext db = new doXZDbContext();
        private XZ_Salary salary = new XZ_Salary();
        private XZ_GswhHelper Gswh = new XZ_GswhHelper();
        /// <summary>
        /// 薪酬列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Get(XZ_DbjlModelGetVm model)
        {
            var entity = db.XZ_Dbjl.AsQueryable();
            if (model != null)
            {

            }
            var result = await entity.OrderByDescending(p => p.Id).ToPagedListAsync(model);
            return Ok(new { total = entity.Count(), rows = result });
        }
        /// <summary>
        /// 薪酬添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Post(XZ_DbjlModelPostVm model)
        {
            if (!ModelState.IsValid) this.Error("请求参数不正确");
            XZ_DbjlModel entity = new XZ_DbjlModel();
            
            entity.UserId = "admin";
            entity.RealyMoney = model.RealyMoney;
            entity.CreateTime = DateTime.Now;
            entity.Qyxz = model.Qyxz;
            entity.Xl = model.Xl;
            if (entity.Qyxz=="国有企业")
            {
                if (model.Xl == "博士")
                {
                    Gswh.x1 = "1";
                    Gswh.x2 = "0";
                    Gswh.x3 = "0";
                    Gswh.x4 = "1";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2)-280;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2)-260;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2)-300;
                }
                else if (model.Xl == "硕士")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "1";
                    Gswh.x3 = "0";
                    Gswh.x4 = "1";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) - 280;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) - 260;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) - 300;
                }
                else if (model.Xl == "大学本科")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "0";
                    Gswh.x3 = "1";
                    Gswh.x4 = "1";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) - 280;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) - 260;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) - 300;
                }
            }
            if ( entity.Qyxz == "民营企业")
            {
                if (model.Xl == "博士")
                {
                    Gswh.x1 = "1";
                    Gswh.x2 = "0";
                    Gswh.x3 = "0";
                    Gswh.x4 = "1";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) +120;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) +150;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) +100;
                }
                else if (model.Xl == "硕士")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "1";
                    Gswh.x3 = "0";
                    Gswh.x4 = "1";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 120;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 150;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 100;
                }
                else if (model.Xl == "大学本科")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "0";
                    Gswh.x3 = "1";
                    Gswh.x4 = "1";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 120;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 150;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 100;
                }
            }
            if (entity.Qyxz == "外资企业" )
            {
                if (model.Xl == "博士")
                {
                    Gswh.x1 = "1";
                    Gswh.x2 = "0";
                    Gswh.x3 = "0";
                    Gswh.x4 = "0";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 310;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 350;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 300;
                }
                else if (model.Xl == "硕士")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "1";
                    Gswh.x3 = "0";
                    Gswh.x4 = "0";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 310;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 350;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 300;
                }
                else if (model.Xl == "大学本科")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "0";
                    Gswh.x3 = "1";
                    Gswh.x4 = "0";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 310;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 350;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 300;
                }
            }
            if ( entity.Qyxz == "混合所有制")
            {
                if (model.Xl == "博士")
                {
                    Gswh.x1 = "1";
                    Gswh.x2 = "0";
                    Gswh.x3 = "0";
                    Gswh.x4 = "0";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 320;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 340;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 290;
                }
                else if (model.Xl == "硕士")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "1";
                    Gswh.x3 = "0";
                    Gswh.x4 = "0";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 320;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 340;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 290;
                }
                else if (model.Xl == "大学本科")
                {
                    Gswh.x1 = "0";
                    Gswh.x2 = "0";
                    Gswh.x3 = "1";
                    Gswh.x4 = "0";
                    entity.Zws = Math.Round(salary.XZ_Zws(Gswh), 2) + 320;
                    entity.Gws = Math.Round(salary.XZ_Gws(Gswh), 2) + 340;
                    entity.Dws = Math.Round(salary.XZ_Dws(Gswh), 2) + 290;
                }
            }
            //entity.Zws = Math.Round(salary.XZ_Zws(Gswh),2);
            //entity.Gws = Math.Round(salary.XZ_Gws(Gswh),2);
            //entity.Dws = Math.Round(salary.XZ_Dws(Gswh),2);
            //if (model.RealyMoney>gws)
            //{
            //    entity.Dbjg = "真实工资高于高位数";
            //}
            //if (model.RealyMoney<gws&&model.RealyMoney>zws)
            //{
            //    entity.Dbjg = "真实位于工资高位数跟中位数之间";
            //}
            //if (model.RealyMoney<zws&&model.RealyMoney>dws)
            //{
            //    entity.Dbjg = "真实位于工资中位数跟低位数之间";
            //}
            //if (model.RealyMoney<dws)
            //{
            //    entity.Dbjg = "真实工资低于低位数";
            //}
            double a = entity.RealyMoney - entity.Dws;
            double b = entity.Gws - entity.Dws;
            double c = Math.Round(a / b, 2);
            entity.Dbjg = c.ToString();
            db.XZ_Dbjl.Add(entity);
            await db.SaveChangesAsync();
            return Ok(entity.Dbjg);
        }
        /// <summary>
        /// 删除薪酬
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Delete(int id)
        {
            var entity = await db.XZ_Dbjl.FindAsync(id);
            db.XZ_Dbjl.Remove(entity);
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }
    }
    public class XZ_DbjlModelPostVm
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 用户编号
        /// </summary>
        public string UserId { get; set; }
        /// <summary>
        /// 真实工资
        /// </summary>
        public double RealyMoney { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 对比结果
        /// </summary>
        public string Dbjg { get; set; }
        /// <summary>
        /// 高位数
        /// </summary>
        public double Gws { get; set; }
        /// <summary>
        /// 低位数
        /// </summary>
        public double Dws { get; set; }
        /// <summary>
        /// 中位数
        /// </summary>
        public double Zws { get; set; }
        /// <summary>
        /// 企业性质
        /// </summary>
        public string Qyxz { get; set; }
        /// <summary>
        /// 学历
        /// </summary>
        public string Xl { get; set; }
    }
    public class XZ_DbjlModelGetVm:VmBase
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 用户编号
        /// </summary>
        public string UserId { get; set; }
        /// <summary>
        /// 真实工资
        /// </summary>
        public double RealyMoney { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 对比结果
        /// </summary>
        public string Dbjg { get; set; }
        /// <summary>
        /// 企业性质
        /// </summary>
        public string Qyxz { get; set; }
        /// <summary>
        /// 学历
        /// </summary>
        public string Xl { get; set; }
    }
}
